<template>
  <el-row>
    <el-col :span="24">
        <el-breadcrumb  separator="/">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>活动管理</el-breadcrumb-item>
          <el-breadcrumb-item>活动列表</el-breadcrumb-item>
          <el-breadcrumb-item>活动详情</el-breadcrumb-item>
        </el-breadcrumb>
      </el-col>
      <el-col :span="24">
        <el-button type="primary" plain>添加角色</el-button>
      </el-col>
      <el-col :span="24">
        <el-table
          :data="roleList"
          style="width: 100%"
          border>
          <el-table-column type="expand">
            <template slot-scope="scope">
              <el-row v-for="item in scope.row.children1">
                <el-col :span="4"  :key="item.id">
                  <el-tag closable @close="deleteRoleRight(scope.row.id,item.id)">{{item.authName}}</el-tag>
                </el-col>
                <el-col :span="20">
                  <el-row v-for="item1 in item.children" :key="item1.id">
                    <el-col :span="3">
                      <el-tag closable type="success" @close="deleteRoleRight(scope.row.id,item1.id)">{{item1.authName}}</el-tag>
                    </el-col>
                    <el-col :span="21" >
                      <el-tag closable type="warning" v-for="item2 in item1.children" :key="item2.id" @close="deleteRoleRight(scope.row.id,item2.id)">{{item2.authName}}</el-tag>
                    </el-col>
                  </el-row>
                </el-col>
              </el-row>
            </template>
          </el-table-column>
          <el-table-column
            label="角色名称"
            prop="roleName">
          </el-table-column>
          <el-table-column
            label="描述"
            prop="roleDesc">
          </el-table-column>
          <el-table-column label="操作">
            <template>
              <el-button
                plain
                size="mini"
                icon="el-icon-edit"
                type="primary"
                ></el-button>
              <el-button
                plain
                size="mini"
                type="danger"
                icon="el-icon-delete"
                ></el-button>
              <el-button
                plain
                size="mini"
                type="warning"
                icon="el-icon-check"
                ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
      <el-row>
        <el-col :span=24>
          <el-dialog title="授权角色" :visible.sync="dialogFormVisible">
            <el-tree
              :data="data2"
              show-checkbox
              node-key="id"
              :default-expanded-keys="[2, 3]"
              :default-checked-keys="[5]"
              :props="defaultProps">
            </el-tree>
            <div slot="footer" class="dialog-footer">
              <el-button @click="dialogFormVisible=false">取消</el-button>
              <el-button type="primary" @click="dialogFormVisible=false">确定</el-button>
            </div>
          </el-dialog>
        </el-col>
      </el-row>
  </el-row>
</template>
<script>
export default {
  data () {
    return {
      roleList:[],  
      dialogFormVisible:true,
      data2: [{
          id: 1,
          label: '一级 1',
          children: [{
            id: 4,
            label: '二级 1-1',
            children: [{
              id: 9,
              label: '三级 1-1-1'
            }, {
              id: 10,
              label: '三级 1-1-2'
            }]
          }]
        }, {
          id: 2,
          label: '一级 2',
          children: [{
            id: 5,
            label: '二级 2-1'
          }, {
            id: 6,
            label: '二级 2-2'
          }]
        }, {
          id: 3,
          label: '一级 3',
          children: [{
            id: 7,
            label: '二级 3-1'
          }, {
            id: 8,
            label: '二级 3-2'
          }]
        }],
        defaultProps: {
          children: 'children',
          label: 'label'
        }
    };
  },
  created() {
    this.getRoleList()
  },
  methods: {
    getRoleList(){
      this.$http.get('roles').then(res=>{
        console.log(res);
        if(res.data.meta.status == 200){
          res.data.data.forEach(item=>{
            item.children1 = item.children
            item.children = []
          })
          this.roleList = res.data.data 
        }
      })
    },
    deleteRoleRight(roleId,rightId){
      console.log(roleId,rightId);
      this.$http.delete(`roles/${roleId}/rights/${rightId}`).then(res=>{
        if(res.data.meta.status == 200){
          this.getRoleList()
        }
      })
    }
  },
}
</script>
<style lang="scss" scoped>
td > .el-row{
  margin-bottom:10px;
  padding-bottom:10px;
  border-bottom:1px solid #ccc;
}
</style>